﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cotizav.CONEXION;
using System.Transactions;
using System.Windows.Forms;
using System.Data.Linq;

namespace Cotizav
{
    public class CtrlDistrito
    {
        ConexionDataContext conexion;
        
        internal List<DISTRITO> getListDistrito(string idProvincia)
        {               
            using (conexion = new ConexionDataContext())
            {
                try
                {
                    DataLoadOptions dlo = new DataLoadOptions();
                    dlo.LoadWith<DISTRITO>(d => d.PROVINCIA);
                    conexion.LoadOptions = dlo;

                    return conexion.DISTRITO.Where(x => x.idProvincia == idProvincia && x.eliminado == 0).ToList();
                }
                catch (Exception)
                {
                    
                    throw;
                }                           
            }
        }

        internal System.Windows.Forms.DialogResult registrarDistrito(DISTRITO dist)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        DialogResult result = DialogResult.Cancel;

                        //insertamos el id
                        int correlativoDistrito = conexion.COTIZAV_CORRELATIVO.Single(x => x.IDENTIFICADOR == "COTIZAV").DISTRITO.Value;
                        string num = Convert.ToString(correlativoDistrito);
                        string numero = num.Trim().PadLeft(10, '0');
                        string idDistrito = "DIST_" + numero;

                        dist.idDistrito = idDistrito;

                        //insertamos el sector
                        conexion.DISTRITO.InsertOnSubmit(dist);
                        conexion.SubmitChanges();
                        conexion.increm_ContDistrito();

                        transactionScope.Complete();
                        result = DialogResult.OK;

                        return result;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        internal DialogResult actualizarDistrito(DISTRITO dis)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        CONEXION.DISTRITO distr = conexion.DISTRITO.Single(x => x.idDistrito == dis.idDistrito);
                        DialogResult result = DialogResult.Cancel;
                        //actualizamos
                        distr.nombreDistrito = dis.nombreDistrito;
                        distr.eliminado = dis.eliminado;

                        conexion.SubmitChanges();
                        transactionScope.Complete();
                        result = DialogResult.OK;

                        return result;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        internal void eliminarDistrito(DISTRITO dis)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                using (conexion = new ConexionDataContext())
                {
                    try
                    {
                        CONEXION.DISTRITO dist = conexion.DISTRITO.Single(x => x.idDistrito == dis.idDistrito);
                        //actualizamos
                        dist.eliminado = dis.eliminado;

                        conexion.SubmitChanges();
                    transactionScope.Complete();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }
    }
}
